package com.zxm.sunshineaiary.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.http.protocol.ExecutionContext;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import android.util.Log;

import com.zxm.sunshineaiary.info.DateInfo;

/**
 * ���ƴ洢�ļ�����
 * 
 * @author Administrator
 * 
 */
public class StorageFile {

	private String sdDir;
	public final String PNG = ".png";
	public final String DOC = ".doc";

	private static StorageFile instance = null;

	public static StorageFile getInsance() {
		if (null == instance) {
			instance = new StorageFile();
		}
		return instance;
	}

	private StorageFile() {
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory().getPath()
					+ "/SunshineDiary";
			;
		}
		isFileExists(sdDir);
	}

	/**
	 * ��ߴ������ļ�·���ַ�����жϲ������ļ���
	 * 
	 * @param dateInfo
	 *            �ļ�����·������
	 * @param bitmap
	 *            ��Ҫ�����BitmapͼƬ
	 * @param name
	 *            ������sd�ϵ��ļ���
	 * @throws IOException
	 */
	public void saveBitmapOfSd(DateInfo dateInfo, Bitmap bitmap, String name)
			throws IOException {

		String path = createFile(dateInfo);
		FileOutputStream fos;

		StringBuffer sdPathBuf = new StringBuffer();
		sdPathBuf.append(path);
		sdPathBuf.append("/");
		sdPathBuf.append(name);
		sdPathBuf.append(PNG);

		fos = new FileOutputStream(sdPathBuf.toString());
		bitmap.compress(CompressFormat.PNG, 5, fos);
		fos.close();
	}

	public CharSequence getStringOfSD(DateInfo dateInfo, String name) {
		CharSequence fileContent = null;
		FileReader fr = null;
		BufferedReader br = null;
		try {
			String path = getFilePath(dateInfo, name, DOC);
			fr = new FileReader(path);
			if (null == fr) {
				return null;
			}
			br = new BufferedReader(fr);
			String line = "";
			StringBuffer buffer = new StringBuffer();
			while ((line = br.readLine()) != null) {
				buffer.append(line + " \n");
			}
			fileContent = buffer.toString();
		} catch (Exception e) {

		}

		return fileContent;
	}

	/**
	 * ���·�����ļ������ھʹ���
	 * 
	 * @param path
	 */
	private void isFileExists(String path) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}
	}

	public boolean saveText(DateInfo dateInfo, String name, String eidtText)
			throws IOException {
		String path = createFile(dateInfo);
		StringBuffer sdPathBuf = new StringBuffer();
		sdPathBuf.append(path);
		sdPathBuf.append("/");
		sdPathBuf.append(name);
		sdPathBuf.append(DOC);

		FileOutputStream fos;
		fos = new FileOutputStream(sdPathBuf.toString());
		OutputStreamWriter writer = new OutputStreamWriter(fos);
		writer.write(eidtText);
		writer.flush();
		writer.close();
		fos.close();

		return false;
	}

	/**
	 * ���ͼƬ·��
	 * 
	 * @param i
	 * @param j
	 * @param k
	 * @param imageNum
	 * @return
	 */
	public String getFilePath(DateInfo dateInfo, String imageNum,
			String fileType) {
		String type = fileType;

		StringBuffer sdPathBuf = new StringBuffer();
		sdPathBuf.append(sdDir);
		sdPathBuf.append("/");
		sdPathBuf.append(dateInfo.year);
		sdPathBuf.append("/");
		sdPathBuf.append(dateInfo.month);
		sdPathBuf.append("/");
		sdPathBuf.append(dateInfo.day);
		sdPathBuf.append("/");
		sdPathBuf.append(imageNum);
		sdPathBuf.append(type);
		return sdPathBuf.toString();
	}

	/**
	 * ���ж��ļ��Ƿ���ڣ������ھͳ�����
	 * 
	 * @param dateInfo
	 */
	public String createFile(DateInfo dateInfo) {

		StringBuffer sdPathBuf = new StringBuffer();
		sdPathBuf.append(sdDir);
		sdPathBuf.append("/");
		sdPathBuf.append(dateInfo.year);
		String yearFile = sdPathBuf.toString();
		sdPathBuf.append("/");
		sdPathBuf.append(dateInfo.month);
		String monthFile = sdPathBuf.toString();
		sdPathBuf.append("/");
		sdPathBuf.append(dateInfo.day);
		String dayFile = sdPathBuf.toString();
		Log.v("xxx", "createFile===" + sdPathBuf.toString());

		isFileExists(yearFile);
		isFileExists(monthFile);
		isFileExists(dayFile);

		return dayFile;
	}

	/**
	 * 删除文件夹
	 * 
	 * @param dateInfo
	 */
	public void deleteFile(DateInfo dateInfo) {
		String path = createFile(dateInfo);

		File file = new File(path);
		try {
			Runtime.getRuntime().exec("rm -r " + path);
			Log.v("xxx", "rm " + path);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
